package com.polaris.live.dialog

import android.content.Context
import android.view.View
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.enums.PopupPosition
import com.polaris.live.R
import com.polaris.live.databinding.PopupTogetherDetailsBinding
import com.polaris.live.resp.back_resp.UserConsumableRewardV2Resp
import com.polaris.live.utils.route.RouterUtils

/**
 * TogetherDetailsDialog
 *
 * @author Created by 天晴 on 2024/11/12 15:02
 * @since 1.0.0
 **/
class TogetherDetailsDialog(
    context: Context,
    private val data: UserConsumableRewardV2Resp,
) : AttachPopupView(context) {

    private lateinit var mBinding: PopupTogetherDetailsBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_together_details
    }

    override fun onCreate() {
        mBinding = PopupTogetherDetailsBinding.bind(popupImplView)
        mBinding.titleTv.text = data.popupTitle
        val highlight = data.popupHighlight?.highlight
        if (highlight == null) {
            mBinding.msgTv.text = data.popupText
        } else {
            RouterUtils.handleText(mBinding.msgTv, data.popupText, highlight)
        }

    }

    override fun doAttach() {
        super.doAttach()
        if (popupInfo == null) {
            return
        }
        val rect = popupInfo?.atViewRect
        if (rect != null) {
            val x = popupContentView.translationX
            val nx = rect.left + (rect.right - rect.left) / 2
            mBinding.triangleImg.translationX = nx - x
        }
    }

    companion object {

        fun show(
            context: Context,
            view: View,
            data: UserConsumableRewardV2Resp,
        ) {
            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .enableDrag(false)
                .atView(view)
                .popupPosition(PopupPosition.Top)
                .customAnimator(null)
                .popupAnimation(PopupAnimation.NoAnimation)
                .hasShadowBg(false)
                .asCustom(TogetherDetailsDialog(context, data))
                .show()
        }
    }
}

